package com.chenggong.window.activity.module

import android.animation.ArgbEvaluator
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.text.ClipboardManager
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.core.view.isVisible
import com.chenggong.animation.utils.AnimationUtils
import com.chenggong.log.LogDebugUtil
import com.chenggong.window.R
import com.chenggong.window.base.BaseActivity
import com.chenggong.window.databinding.ActivityModuleAnimationBinding

class ModuleAnimationActivity: BaseActivity<ActivityModuleAnimationBinding>() {

    private var heartBeat: ObjectAnimator? = null
    private var isHeartBeat: Boolean = false
    private lateinit var adapter: ArrayAdapter<String>
    override val layoutId: Int
        get() = R.layout.activity_module_animation

    override fun initView(savedInstanceState: Bundle?) {

        val data = arrayOf(
            getString(R.string.text_argb_evaluator),
            getString(R.string.text_shake),
            getString(R.string.text_zoom),
            getString(R.string.text_heart_beat),
        )
        adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data)
        val listView = findViewById<ListView>(R.id.list)
        listView.adapter = adapter
        listView.onItemClickListener = AdapterView.OnItemClickListener { _, view, position, _ ->
            onItemClick(view,position)
        }

        mDataBinding.ivClose.setOnClickListener {
            mDataBinding.llInstructions.isVisible = false
            if(animator.isRunning){
                animator.cancel()
            }
        }
        mDataBinding.ivCopy.setOnClickListener {
            copyString(mDataBinding.tvContent.text.toString())
        }
    }

    private val animator: ValueAnimator by lazy {
        ObjectAnimator.ofInt(mDataBinding.tvContent,
            "textColor",
            Color.parseColor("#88333833"),
            Color.parseColor("#88ca0007"),
            Color.parseColor("#880333dc"),
            Color.parseColor("#88089905")).apply {
            duration = 2000
            setEvaluator(ArgbEvaluator())
            repeatCount = ValueAnimator.INFINITE
            repeatMode = ValueAnimator.REVERSE
        }

    }

    private fun onItemClick(view: View, position: Int) {
        val value = adapter.getItem(position)
        when(value){
            getString(R.string.text_argb_evaluator) -> {
                mDataBinding.llInstructions.isVisible = true
                mDataBinding.tvContent.text = "TextView text = findViewById(233);\n" +
                        "ValueAnimator animator = ObjectAnimator.ofInt(\n" +
                        "text,\n" +
                        "\"textColor\",\n" +
                        "Color.parseColor(\"#88333833\"), \n" +
                        "Color.parseColor(\"#88ca0007\"), \n" +
                        "Color.parseColor(\"#880333dc\"), \n" +
                        "Color.parseColor(\"#88089905\"));\n" +
                        "animator.setDuration(2000);\n" +
                        "animator.setEvaluator(new ArgbEvaluator());\n" +
                        "animator.setRepeatCount(ValueAnimator.INFINITE);\n" +
                        "animator.setRepeatMode(ValueAnimator.REVERSE);\n" +
                        "animator.start();";
                animator.start()
            }
            getString(R.string.text_shake) -> {
                AnimationUtils.shake(mDataBinding.root)
                mDataBinding.llInstructions.isVisible = true
                mDataBinding.tvContent.text = "fun shake(\n" +
                        "        view: View,\n" +
                        "        shakeDegrees: Float = 2.0f,\n" +
                        "        duration: Long = 800L\n" +
                        "    ) {\n" +
                        "        if (view == null) {\n" +
                        "            return\n" +
                        "        }\n" +
                        "        //先往左再往右\n" +
                        "        val rotateValuesHolder = PropertyValuesHolder.ofKeyframe(\n" +
                        "            View.ROTATION,\n" +
                        "            Keyframe.ofFloat(0f, 0f),\n" +
                        "            Keyframe.ofFloat(0.2f, -shakeDegrees),\n" +
                        "            Keyframe.ofFloat(0.4f, shakeDegrees),\n" +
                        "            Keyframe.ofFloat(0.6f, -shakeDegrees),\n" +
                        "            Keyframe.ofFloat(0.8f, shakeDegrees),\n" +
                        "            Keyframe.ofFloat(1.0f, 0f)\n" +
                        "        )\n" +
                        "        val objectAnimator = ObjectAnimator.ofPropertyValuesHolder(\n" +
                        "            view,\n" +
                        "            rotateValuesHolder\n" +
                        "        )\n" +
                        "        objectAnimator.duration = duration\n" +
                        "        objectAnimator.start()\n" +
                        "    }"
            }
            getString(R.string.text_zoom) -> {
                AnimationUtils.zoom(mDataBinding.root)
                mDataBinding.llInstructions.isVisible = true
                mDataBinding.tvContent.text = "fun zoom(\n" +
                        "        view: View,\n" +
                        "        scaleSmall: Float = 0.8f,\n" +
                        "        scaleLarge: Float = 1.0f,\n" +
                        "        duration: Long = 800L\n" +
                        "    ) {\n" +
                        "        if (view == null) {\n" +
                        "            return\n" +
                        "        }\n" +
                        "        //先变小后变大\n" +
                        "        val scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(\n" +
                        "            View.SCALE_X,\n" +
                        "            Keyframe.ofFloat(0f, 1.0f),\n" +
                        "            Keyframe.ofFloat(0.25f, scaleSmall),\n" +
                        "            Keyframe.ofFloat(0.5f, scaleLarge),\n" +
                        "            Keyframe.ofFloat(0.75f, scaleSmall),\n" +
                        "            Keyframe.ofFloat(1.0f, 1.0f)\n" +
                        "        )\n" +
                        "        val scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(\n" +
                        "            View.SCALE_Y,\n" +
                        "            Keyframe.ofFloat(0f, 1.0f),\n" +
                        "            Keyframe.ofFloat(0.25f, scaleSmall),\n" +
                        "            Keyframe.ofFloat(0.5f, scaleLarge),\n" +
                        "            Keyframe.ofFloat(0.75f, scaleSmall),\n" +
                        "            Keyframe.ofFloat(1.0f, 1.0f)\n" +
                        "        )\n" +
                        "        val objectAnimator = ObjectAnimator.ofPropertyValuesHolder(\n" +
                        "            view,\n" +
                        "            scaleXValuesHolder,\n" +
                        "            scaleYValuesHolder\n" +
                        "        )\n" +
                        "        objectAnimator.duration = duration\n" +
                        "        objectAnimator.start()\n" +
                        "    }"
            }
            getString(R.string.text_heart_beat) -> {
                isHeartBeat = !isHeartBeat
                if(isHeartBeat){
                    heartBeat = AnimationUtils.heartBeat(view)

                    mDataBinding.llInstructions.isVisible = true
                    mDataBinding.tvContent.text = "fun heartBeat(view: View):ObjectAnimator {\n" +
                            "        val scaleX = PropertyValuesHolder.ofFloat( View.SCALE_X, 1.0f, 0.8f, 1.0f)\n" +
                            "        val scaleY = PropertyValuesHolder.ofFloat( View.SCALE_Y, 1.0f, 0.8f, 1.0f)\n" +
                            "        val anim = ObjectAnimator.ofPropertyValuesHolder(view, scaleX, scaleY)\n" +
                            "        anim.duration = 1000\n" +
                            "        anim.repeatCount = ValueAnimator.INFINITE\n" +
                            "        anim.interpolator = DecelerateInterpolator()\n" +
                            "        anim.startDelay = 100\n" +
                            "        anim.start()\n" +
                            "        //复原\n" +
                            "        anim.addListener(object : Animator.AnimatorListener {\n" +
                            "            override fun onAnimationStart(animation: Animator?) {}\n" +
                            "            override fun onAnimationEnd(animation: Animator?) {\n" +
                            "                view.scaleX = 1.0f\n" +
                            "                view.scaleY = 1.0f\n" +
                            "            }\n" +
                            "            override fun onAnimationCancel(animation: Animator?) {\n" +
                            "                view.scaleX = 1.0f\n" +
                            "                view.scaleY = 1.0f\n" +
                            "            }\n" +
                            "            override fun onAnimationRepeat(animation: Animator?) {}\n" +
                            "\n" +
                            "        })\n" +
                            "        return anim\n" +
                            "    }"
                }else{
                    heartBeat?.cancel()
                }
            }
        }
        LogDebugUtil.d(value)
    }

    override fun onBackPressed() {
        if(mDataBinding.llInstructions.isVisible){
            mDataBinding.llInstructions.isVisible = false
            if(animator.isRunning){
                animator.cancel()
            }
            return
        }
        super.onBackPressed()
    }

    private fun copyString(content: String) {
        val cm = this.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        cm.text = content
        Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show()
    }
}